<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主键" prop="orderGoodId">
        <el-input
          v-model="queryParams.orderGoodId"
          placeholder="请输入主键"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主键" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入主键"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主键" prop="goodId">
        <el-input
          v-model="queryParams.goodId"
          placeholder="请输入主键"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入商品价格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入商品码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存数量" prop="store">
        <el-input
          v-model="queryParams.store"
          placeholder="请输入库存数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否为新品，0为新品" prop="isNew">
        <el-input
          v-model="queryParams.isNew"
          placeholder="请输入是否为新品，0为新品"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址id" prop="addressBookId">
        <el-input
          v-model="queryParams.addressBookId"
          placeholder="请输入地址id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入收货人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="provinceName">
        <el-input
          v-model="queryParams.provinceName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="cityName">
        <el-input
          v-model="queryParams.cityName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="districtName">
        <el-input
          v-model="queryParams.districtName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实收金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入实收金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券id" prop="couponId">
        <el-input
          v-model="queryParams.couponId"
          placeholder="请输入优惠券id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['mixuebingcheng:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mixuebingcheng:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mixuebingcheng:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mixuebingcheng:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="主键" align="center" prop="orderGoodId" />
      <el-table-column label="主键" align="center" prop="orderId" />
      <el-table-column label="主键" align="center" prop="goodId" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品价格" align="center" prop="price" />
      <el-table-column label="图片" align="center" prop="image" width="100">
          <template #default="scope">
              <image-preview :src="scope.row.image" :width="50" :height="50"/>
          </template>
      </el-table-column>
      <el-table-column label="商品码" align="center" prop="code" />
      <el-table-column label="库存数量" align="center" prop="store" />
      <el-table-column label="是否为新品，0为新品" align="center" prop="isNew" />
      <el-table-column label="地址id" align="center" prop="addressBookId" />
      <el-table-column label="收货人" align="center" prop="consignee" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="" align="center" prop="areaCode" />
      <el-table-column label="名称" align="center" prop="provinceName" />
      <el-table-column label="名称" align="center" prop="cityName" />
      <el-table-column label="名称" align="center" prop="districtName" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="实收金额" align="center" prop="amount" />
      <el-table-column label="订单状态 1待付款，2待派送，3已派送，4已完成" align="center" prop="status" />
      <el-table-column label="优惠券id" align="center" prop="couponId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mixuebingcheng:order:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mixuebingcheng:order:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改VIEW对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="主键" prop="orderGoodId">
          <el-input v-model="form.orderGoodId" placeholder="请输入主键" />
        </el-form-item>
        <el-form-item label="主键" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入主键" />
        </el-form-item>
        <el-form-item label="主键" prop="goodId">
          <el-input v-model="form.goodId" placeholder="请输入主键" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="商品码" prop="code">
          <el-input v-model="form.code" placeholder="请输入商品码" />
        </el-form-item>
        <el-form-item label="库存数量" prop="store">
          <el-input v-model="form.store" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="是否为新品，0为新品" prop="isNew">
          <el-input v-model="form.isNew" placeholder="请输入是否为新品，0为新品" />
        </el-form-item>
        <el-form-item label="地址id" prop="addressBookId">
          <el-input v-model="form.addressBookId" placeholder="请输入地址id" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="名称" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="名称" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="名称" prop="districtName">
          <el-input v-model="form.districtName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="实收金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="优惠券id" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/mixuebingcheng/order";

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    orderGoodId: undefined,
    orderId: undefined,
    goodId: undefined,
    name: undefined,
    price: undefined,
    image: undefined,
    code: undefined,
    store: undefined,
    isNew: undefined,
    addressBookId: undefined,
    consignee: undefined,
    phone: undefined,
    areaCode: undefined,
    provinceName: undefined,
    cityName: undefined,
    districtName: undefined,
    number: undefined,
    amount: undefined,
    status: undefined,
    couponId: undefined
  },
  rules: {
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
    orderGoodId: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    goodId: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "商品价格不能为空", trigger: "blur" }
    ],
    image: [
      { required: true, message: "图片不能为空", trigger: "blur" }
    ],
    code: [
      { required: true, message: "商品码不能为空", trigger: "blur" }
    ],
    store: [
      { required: true, message: "库存数量不能为空", trigger: "blur" }
    ],
    isNew: [
      { required: true, message: "是否为新品，0为新品不能为空", trigger: "blur" }
    ],
    addressBookId: [
      { required: true, message: "地址id不能为空", trigger: "blur" }
    ],
    consignee: [
      { required: true, message: "收货人不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "手机号不能为空", trigger: "blur" }
    ],
    areaCode: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    provinceName: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    cityName: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    districtName: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    number: [
      { required: true, message: "数量不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "实收金额不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "订单状态 1待付款，2待派送，3已派送，4已完成不能为空", trigger: "change" }
    ],
    couponId: [
      { required: true, message: "优惠券id不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询VIEW列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    orderGoodId: null,
    orderId: null,
    goodId: null,
    name: null,
    price: null,
    image: null,
    code: null,
    store: null,
    isNew: null,
    addressBookId: null,
    consignee: null,
    phone: null,
    areaCode: null,
    provinceName: null,
    cityName: null,
    districtName: null,
    number: null,
    amount: null,
    status: null,
    couponId: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加VIEW";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _userId = row.userId || ids.value
  getOrder(_userId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改VIEW";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.userId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除VIEW编号为"' + _userIds + '"的数据项？').then(function() {
    loading.value = true;
    return delOrder(_userIds);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mixuebingcheng/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
</script>
